#pragma once

#include <mutex>
#include <string>
#include <memory>
#include <sstream>
#include <glog/logging.h>
#include <condition_variable>
#include <jsoncpp/json/json.h>
#include "basic/tools.h"
#include "basic/UdpServer.h"

class UdpServer;
class Robot {
public:
    Robot(){};
    Robot(uint16_t server_port, uint16_t clinet_port);
    void thread_UDPServer();
    void thread_op();
    void thread_UDPSend();
    void stop();
    void stop(const std::vector<std::string>& node);
    bool isRun();
    void launch_mode(const int mode);
    void launch_cmd(const int cmd);
    void set_map_path(const std::string str) { _map_path = str;}
    std::string get_map_path() {return _map_path;}
private:    
    void func_cmd(const std::string str);

    bool _isRunning;
    int _last_mode;

    // JSON
    Json::Value _va;
    Json::Value _sendVa;
    std::stringstream _sstream;
    Json::FastWriter _styled_writer;
    std::shared_ptr<Json::Reader> _re;

    // 速度计时
    int _vel_pid = 0;
    bool vel_start_flag = false;
    bool _velPause = true;
    float _vel = 0, _yaw = 0;

    // 通信
    const std::string pid_ipv4 = "localhost";  
    uint16_t pid_port = 0;  
    std::mutex _mtx_udploal;
    std::mutex _mtx_message;
    std::condition_variable _udp_cond;
    std::string _ipv4 = "";
    uint16_t _port = 0;
    std::string _message = "";
    std::shared_ptr<UdpServer> _updServer;

    // 
    std::string _map_path;
};
